package ui;

import java.awt.Color;

import java.awt.Graphics;
import java.awt.Insets;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.*;

import javax.imageio.ImageIO;
import javax.swing.JButton;
import javax.swing.JPanel;

import fileIO.Options;
import skin.ButtonSkin;

@SuppressWarnings("serial")
public class ImageButton extends JButton
{
	// private int buttonImagesPerState = 1;
	private ButtonSkin skin;
	private int toggleStyle;			// 0 = no toggling, 1 = true toggling, 2 = tab-like toggling (toggles down whe hit, but doesnt toggle up)
	private boolean isToggledDown;		// flag variable for toggle buttons to see if they are selected
	
	private BufferedImage img_base;
	private BufferedImage rolledImg_base;
	private BufferedImage pressedImg_base;
	private BufferedImage pressedRolledImg_base;
	private BufferedImage disabledImg_base;
	
	private JPanel parentPanel;
	public int coordsX;
	public int coordsY;
	private boolean firstPainting = true;
	
	//private BufferedImage img_slider;				// TODO add a multi image style
	//private BufferedImage rolledImg_slider;
	//private BufferedImage pressedImg_slider;
	
	public ImageButton (String text, boolean toggleButton, boolean tabToggleButton, ButtonSkin buttonSkin) {
		if (tabToggleButton) Initialize (text, 2, null, 0, 0, buttonSkin);
		else if (toggleButton) Initialize (text, 1, null, 0, 0, buttonSkin);
		else Initialize (text, 0, null, 0, 0, buttonSkin);
	}
	public ImageButton (String text, boolean toggleButton, boolean tabToggleButton, JPanel parent, int x, int y, ButtonSkin buttonSkin) {
		if (tabToggleButton) Initialize (text, 2, parent, x, y, buttonSkin);
		else if (toggleButton) Initialize (text, 1, parent, x, y, buttonSkin);
		else Initialize (text, 0, parent, x, y, buttonSkin);
	}	
	private void Initialize (String text, int toggling, JPanel parent, int x, int y, ButtonSkin buttonSkin) {// object initilization
		toggleStyle = toggling;
		isToggledDown = false;
		parentPanel = parent;
	
		skin = buttonSkin;
		try {
			img_base =  ImageIO.read(new File(buttonSkin.getImgPath (0,0)));
			rolledImg_base =  ImageIO.read(new File(buttonSkin.getImgPath (1,0)));
			pressedImg_base =  ImageIO.read(new File(buttonSkin.getImgPath (2,0)));
			pressedRolledImg_base =  ImageIO.read(new File(buttonSkin.getImgPath (3,0)));
			disabledImg_base =  ImageIO.read(new File(buttonSkin.getImgPath (4,0)));
			
			/* TODO add a multi image style
			if (buttonSkin.getButtonImagesPerState()>1) {
				img_slider = ImageIO.read(new File(buttonSkin.getImgPath (0,1)));
				rolledImg_slider = ImageIO.read(new File(buttonSkin.getImgPath (1,1)));
				pressedImg_slider = ImageIO.read(new File(buttonSkin.getImgPath (2,1)));
			}*/
			
			setSize(img_base.getWidth(null), img_base.getHeight(null));
		} catch (IOException e) {
			System.out.println ("ERROR in ImageButton: cannot initialize, IOException while reading image files.");
			e.printStackTrace();
		}
				
		// TODO allow for diff sizes and the other half of the damn button
	    setMargin(new Insets(0, 0, 0, 0));	//this is more or less the murdered version of the vanilla constructor
	    setIconTextGap(0);
	    setForeground (Color.white);
	    setBorderPainted(false);
	    setBorder(null);
	    setText(text);
		coordsX = x;
		coordsY = y;
	}

	
	// === SETTERS ================================================================================
	public void setToggledDown (boolean value) {
		isToggledDown = value;
		repaint();
	}
	
	// === DRAWING METHODS ========================================================================		
	public void paintComponent(Graphics g)
	{
		if (firstPainting)
			firstPainting = false;
		else
			parentPanel.repaint(coordsX, coordsY, coordsX + this.getWidth(), coordsY + this.getHeight());
		
		if (!isEnabled()) drawState_disabled (g);					// disabled state
		else if (getModel().isPressed()) {							// pressed state
			drawState_pressed (g);
			if (toggleStyle == 1) isToggledDown = !isToggledDown;	// for toggle-buttons, change state
			else if (toggleStyle == 2) isToggledDown = true;		// for tag-toggles, make sure its hit
		}
		else if (getModel().isRollover()) {							// rolledOver state
			if (isToggledDown) {
				if (toggleStyle == 2) drawState_pressed (g);		// tab-toggles dont light up when down
				else drawState_pressedRolled (g);
			}
			else drawState_rolled (g);		
		}
		else {														// normal state
			if (isToggledDown) drawState_pressed (g);
			else drawState_normal (g);				  			  		
		}
	}
	// TODO allow for diff sizes and the other half of the damn button - for all of these draw methods
	private void drawState_disabled (Graphics g) {
		g.setColor(skin.getFontColor(4));
		g.drawImage(disabledImg_base, 0, 0, this);
		g.drawString(getText(), 20, disabledImg_base.getHeight()/2+5);
	}
	private void drawState_normal (Graphics g) {
		g.setColor(skin.getFontColor(0));
		g.drawImage(img_base, 0, 0, this);
		g.drawString(getText(), 20, img_base.getHeight()/2+5);
	}
	private void drawState_rolled (Graphics g) {		
		g.setColor(skin.getFontColor(1));
		g.drawImage(rolledImg_base, 0, 0, this);
		g.drawString(getText(), 20, rolledImg_base.getHeight()/2+5);
	}
	private void drawState_pressed (Graphics g) {
		g.setColor(skin.getFontColor(2));
		g.drawImage(pressedImg_base, 0, 0, this);
		g.drawString(getText(), 20, pressedImg_base.getHeight()/2+5);
	}
	private void drawState_pressedRolled (Graphics g) {
		g.setColor(skin.getFontColor(3));
		g.drawImage(pressedRolledImg_base, 0, 0, this);
		g.drawString(getText(), 20, pressedRolledImg_base.getHeight()/2+5);
	}
	// === MAKER ========================================================================
	public static ImageButton spawnButton (String buttonType, String buttonText, String buttonCmd, ActionListener listener, JPanel parentPanel, int x, int y, Options options) {
		ImageButton btn;
		if (buttonType.toLowerCase() == "toggle")
			btn = new ImageButton (buttonText, true, false, parentPanel, x, y, options.skin.button);
		else if (buttonType.toLowerCase() == "tab")
			btn = new ImageButton (buttonText, true, true, parentPanel, x, y, options.skin.button);	
		else 
			btn = new ImageButton (buttonText, false, false, parentPanel, x, y, options.skin.button);
			
		btn.setLocation(x, y);
		if (buttonCmd != "") {
			btn.addActionListener(listener);
			btn.setActionCommand(buttonCmd);
		}
		parentPanel.add(btn);
		return btn;
	}
}
